import { defineStore } from "pinia";

import { store } from "@/store";

export interface DictData {
  label: string;
  title: string;
  value: string | number | boolean;
  cssClass?: string;
}

interface Dict {
  type: {
    [key: string]: DictData[];
  };
}

export const useDictStore = defineStore("dict", () => {
  //state
  const dict = reactive<Dict>({
    type: {},
  });

  //action
  function getDict(key: string) {
    if (key == null || key == "") {
      return "";
    }

    return dict.type[key];
  }

  //set
  function setDict(key: string, value: DictData[]) {
    dict.type[key] = value;
  }

  //remove
  function removeDict(key: string) {
    delete dict.type[key];
  }

  //clear
  function clearDict() {
    dict.type = {};
  }

  return { dict, setDict, getDict, removeDict, clearDict };
});

// 非setup
export function useDictHook() {
  return useDictStore(store);
}
